package com.icepeach.icepeachhomeback.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.icepeach.icepeachhomeback.dto.BindUserRoleDto;
import com.icepeach.icepeachhomeback.entity.Role;
import com.icepeach.icepeachhomeback.exception.Assert;
import com.icepeach.icepeachhomeback.req.RoleListQueryReq;
import com.icepeach.icepeachhomeback.service.RoleService;
import com.icepeach.icepeachhomeback.util.ApiResult;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Resource
    RoleService roleService;

    /**
     * 获取所有角色
     *
     * @return
     */
    @PostMapping("/getAllRole")
    @ResponseBody
    public ApiResult getAllRole() {
        List<Role> list = roleService.list(null);
        return ApiResult.success("查询成功", list);
    }

    /**
     * 根据id获取单个角色
     *
     * @return
     */
    @PostMapping("/getRoleById")
    @ResponseBody
    public ApiResult getRoleById(String id) {
        if (StrUtil.isNotBlank(id)) {
            return ApiResult.success("查询成功", roleService.getById(id));
        } else {
            return ApiResult.failure("请求异常");
        }
    }
 


    /**
     * 分页查询 获取角色
     *
     * @param roleListQueryReq
     * @return
     */
    @PostMapping("/getRolesByPage")
    @ResponseBody
    public ApiResult getRolesByPage(@RequestBody RoleListQueryReq roleListQueryReq) {

        return ApiResult.success("查询成功", roleService.getRolesByPage(roleListQueryReq));
    }

    /**
     * 增加或修改角色
     *
     * @param role
     * @return
     */

    @PostMapping("/saveRole")
    @ResponseBody
    public ApiResult saveRole(@RequestBody Role role) {
        return roleService.mySaveOrUpdate(role);

    }

    @PostMapping("/deleteById")
    @ResponseBody
    public ApiResult deleteById(@RequestBody Role role) {
        boolean result = roleService.removeById(role.getId());

        if (result) {
            return ApiResult.success("成功");
        } else {
            return ApiResult.failure("失败");
        }
    }

    @PostMapping("/del/batch")
    @ResponseBody
    public ApiResult deleteBatch(@RequestBody List<String> ids) {
        roleService.removeByIds(ids);
        return ApiResult.success("成功");
    }

    /**
     * 分配角色
     * @param bindUserRoleDto
     * @return
     */
    @PostMapping("/assignRole")
    @ResponseBody
    public ApiResult assignRole(@RequestBody @Validated BindUserRoleDto bindUserRoleDto) {
        return roleService.assignRole(bindUserRoleDto);
    }

    /**
     * 移除角色
     * @param bindUserRoleDto
     * @return
     */
    @PostMapping("/removeRole")
    @ResponseBody
    public ApiResult removeRole(@RequestBody BindUserRoleDto bindUserRoleDto) {
        Assert.notNull(bindUserRoleDto,"参数不能为空");
        return roleService.removeRole(bindUserRoleDto);
    }

    /**
     * 未分配角色列表
     * @param bindUserRoleDto
     * @return
     */
    @PostMapping("/unBindRoleList")
    @ResponseBody
    public ApiResult<List<Role>> unBindRoleList(@RequestBody BindUserRoleDto bindUserRoleDto) {
        Assert.notNull(bindUserRoleDto,"参数不能为空");
        return roleService.unBindRoleList(bindUserRoleDto.getUserId());
    }

    /**
     * 已分配角色列表
     * @param bindUserRoleDto
     * @return
     */
    @PostMapping("/bindRoleList")
    @ResponseBody
    public ApiResult bindRoleList(@RequestBody BindUserRoleDto bindUserRoleDto) {
        Assert.notNull(bindUserRoleDto,"参数不能为空");
        return roleService.bindRoleList(bindUserRoleDto.getUserId());
    }
}
